# 前端构建阶段
FROM docker.m.daocloud.io/library/node:20-alpine as frontend-builder
WORKDIR /app
# 安装基础依赖
RUN apk add --no-cache python3 make g++

# 配置npm
RUN npm config set registry https://registry.npmmirror.com \
    && npm config set fetch-timeout 600000 \
    && npm config set fetch-retries 3 \
    && npm config set fetch-retry-mintimeout 15000 \
    && npm config set fetch-retry-maxtimeout 60000

# 安装和配置pnpm
RUN npm install -g pnpm --registry=https://registry.npmmirror.com || npm install -g pnpm --registry=https://registry.npm.taobao.org

RUN pnpm config set registry https://registry.npmmirror.com \
    && pnpm config set @beike:registry https://registry.npmmirror.com \
    && pnpm config set network-timeout 600000
COPY ui/package.json ./
RUN pnpm install
COPY ui/ .
RUN pnpm build

# 后端构建阶段
FROM docker.m.daocloud.io/library/debian:bookworm-slim as backend-builder
WORKDIR /app

# 配置apt源（先使用http）
RUN rm -f /etc/apt/sources.list.d/* && echo 'deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware' \
      > /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware' \
      >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware' \
      >> /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates && \
    sed -i 's/http:/https:/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    curl \
    wget \
    gnupg2 \
    && rm -rf /var/lib/apt/lists/*

# 安装JDK和Maven
COPY ./pac/OpenJDK17U-jdk_x64_linux_hotspot_17.0.16_8.tar.gz /tmp/
COPY ./pac/apache-maven-3.9.11-bin.tar.gz /tmp/

# 解压安装JDK和Maven
RUN cd /tmp && \
    tar xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.16_8.tar.gz && \
    mkdir -p /usr/lib/jvm && \
    mv jdk-17.0.16+8 /usr/lib/jvm/ && \
    tar xzf apache-maven-3.9.11-bin.tar.gz && \
    mv apache-maven-3.9.11 /usr/local/ && \
    ln -s /usr/lib/jvm/jdk-17.0.16+8 /usr/lib/jvm/java-17 && \
    rm -rf /tmp/*

# 设置环境变量
ENV JAVA_HOME=/usr/lib/jvm/java-17
ENV M2_HOME=/usr/local/apache-maven-3.9.11
ENV PATH=${JAVA_HOME}/bin:${M2_HOME}/bin:${PATH}

# 复制和构建后端代码
COPY genie-backend/pom.xml .
COPY genie-backend/src ./src
COPY genie-backend/build.sh genie-backend/start.sh ./
RUN chmod +x build.sh start.sh
RUN ./build.sh

# Python 环境准备阶段
FROM docker.m.daocloud.io/library/debian:bookworm-slim as python-base
WORKDIR /app

# 配置apt源（先使用http）
RUN rm -f /etc/apt/sources.list.d/* && echo 'deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware' \
      > /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware' \
      >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware' \
      >> /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates && \
    sed -i 's/http:/https:/g' /etc/apt/sources.list && \
    apt-get update

# 安装编译Python所需的依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libssl-dev \
    zlib1g-dev \
    libbz2-dev \
    libreadline-dev \
    libsqlite3-dev \
    libncurses5-dev \
    libncursesw5-dev \
    xz-utils \
    tk-dev \
    libffi-dev \
    liblzma-dev \
    netcat-openbsd \
    procps \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# 复制并编译Python
COPY ./pac/Python-3.11.0.tgz /tmp/
RUN cd /tmp && \
    tar xzf Python-3.11.0.tgz && \
    cd Python-3.11.0 && \
    ./configure --enable-optimizations && \
    make -j$(nproc) && \
    make install && \
    cd / && \
    rm -rf /tmp/Python-3.11.0* && \
    python3 -m ensurepip && \
    pip3 install --no-cache-dir uv

# 最终运行阶段
FROM docker.m.daocloud.io/library/debian:bookworm-slim

# 配置apt源（先使用http）
RUN rm -f /etc/apt/sources.list.d/* && echo 'deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware' \
      > /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware' \
      >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware' \
      >> /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates && \
    sed -i 's/http:/https:/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    python3 \
    python3-dev \
    python3-pip \
    netcat-openbsd \
    procps \
    curl \
    nodejs \
    npm \
    && rm -rf /var/lib/apt/lists/*

# 配置npm
RUN npm config set registry https://registry.npmmirror.com \
    && npm config set fetch-timeout 600000 \
    && npm config set fetch-retries 3 \
    && npm config set fetch-retry-mintimeout 15000 \
    && npm config set fetch-retry-maxtimeout 60000

# 安装pnpm
RUN npm install -g pnpm --registry=https://registry.npmmirror.com || npm install -g pnpm --registry=https://registry.npm.taobao.org

# 安装JDK
COPY ./pac/OpenJDK17U-jdk_x64_linux_hotspot_17.0.16_8.tar.gz /tmp/
RUN cd /tmp && \
    tar xzf OpenJDK17U-jdk_x64_linux_hotspot_17.0.16_8.tar.gz && \
    mkdir -p /usr/lib/jvm && \
    mv jdk-17.0.16+8 /usr/lib/jvm/ && \
    ln -s /usr/lib/jvm/jdk-17.0.16+8 /usr/lib/jvm/java-17 && \
    rm -rf /tmp/*

# 设置Java环境变量
ENV JAVA_HOME=/usr/lib/jvm/java-17
ENV PATH=${JAVA_HOME}/bin:${PATH}

# 设置工作目录
WORKDIR /app

# 复制前端构建产物
COPY --from=frontend-builder /app/dist /app/ui/dist
COPY --from=frontend-builder /app/package.json /app/ui/package.json
COPY --from=frontend-builder /app/node_modules /app/ui/node_modules

# 复制后端构建产物
COPY --from=backend-builder /app/target /app/backend/target
COPY genie-backend/start.sh /app/backend/
RUN chmod +x /app/backend/start.sh

# 复制 Python 工具和依赖
COPY --from=python-base /usr/local/lib/python3.11 /usr/local/lib/python3.11
COPY --from=python-base /usr/local/bin/uv /usr/local/bin/uv

# 复制 genie-client
WORKDIR /app/client
COPY genie-client/pyproject.toml genie-client/uv.lock ./
COPY genie-client/app ./app
COPY genie-client/main.py genie-client/server.py genie-client/start.sh ./
RUN chmod +x start.sh && \
    uv venv .venv && \
    . .venv/bin/activate && \
    export UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple" && uv sync

# 复制 genie-tool
WORKDIR /app/tool
COPY genie-tool/pyproject.toml genie-tool/uv.lock ./
COPY genie-tool/genie_tool ./genie_tool
COPY genie-tool/server.py genie-tool/start.sh genie-tool/.env_template ./

# 创建虚拟环境并安装依赖
RUN chmod +x start.sh && \
    uv venv .venv && \
    . .venv/bin/activate && \
    export UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple" && uv sync && \
    mkdir -p /data/genie-tool && \
    cp .env_template .env && \
    python -m genie_tool.db.db_engine

# 设置数据卷
VOLUME ["/data/genie-tool"]

# 复制统一启动脚本
WORKDIR /app
COPY start_genie.sh .
RUN chmod +x start_genie.sh

EXPOSE 3000 8080 1601

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:3000 || exit 1

# 启动所有服务
CMD ["./start_genie.sh"]
